package com.lanyan.system.controller.system;

import java.math.BigDecimal;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.lanyan.common.utils.StringUtils;
import com.lanyan.system.domain.*;
import com.lanyan.system.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanyan.common.annotation.Log;
import com.lanyan.common.core.controller.BaseController;
import com.lanyan.common.core.domain.AjaxResult;
import com.lanyan.common.enums.BusinessType;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import com.lanyan.common.utils.poi.ExcelUtil;
import com.lanyan.common.core.page.TableDataInfo;

/**
 * 订单Controller
 *
 * @author lanyan
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/system/orders")
public class OrdersController extends BaseController
{
    @Autowired
    private IOrdersService ordersService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private ICourierService courierService;
    @Autowired
    private IProductService productService;
    @Autowired
    private ISkuService skuService;
    @Autowired
    private IEstateService estateService;
    @Autowired
    private IBuildingService buildingService;
    @Autowired
    private IOrdersItemService ordersItemService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:orders:list')")
    @GetMapping("/list")
    @ApiImplicitParams({
          @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "courierId", value = "所属配送员", dataType = "Long", dataTypeClass = Long.class),
          @ApiImplicitParam(name = "accountId", value = "所属用户", dataType = "Long", dataTypeClass = Long.class),
          @ApiImplicitParam(name = "fetchName", value = "起点用户", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "fetchPhone", value = "起点电话", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "fetchBuilding", value = "起点楼栋", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "fetchDetails", value = "地点地址", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "sendName", value = "终点用户", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "sendPhone", value = "重点电话", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "sendBuilding", value = "终点楼栋", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "sendDetails", value = "终点地址", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "memo", value = "订单备注", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "specialMemo", value = "特殊要求", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "specialMemoImg", value = "特殊要求图片", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "productType", value = "物品类型", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "weight", value = "货物重量", dataType = "Long", dataTypeClass = Long.class),
          @ApiImplicitParam(name = "receivingTime", value = "收货时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "money", value = "订单金额", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
          @ApiImplicitParam(name = "originMoney", value = "订单原价", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
          @ApiImplicitParam(name = "ordersNo", value = "订单编号", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "payNo", value = "支付单号", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "code", value = "收货编号", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "couponId", value = "使用优惠券", dataType = "Long", dataTypeClass = Long.class),
          @ApiImplicitParam(name = "pageNum", value = "第几页", dataType = "int", dataTypeClass = int.class),
          @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", dataTypeClass = int.class),

    })
    public TableDataInfo list(Orders orders)
    {
        startPage();
        List<Orders> list = ordersService.selectOrdersList(orders);
        for (Orders item : list) {
            item.setAccount(accountService.getById(item.getAccountId()));
            item.setCourier(courierService.getById(item.getCourierId()));
            item.setOrdersItemVoList(ordersItemService.lambdaQuery()
                    .eq(OrdersItem::getDelFlag,"0")
                    .eq(OrdersItem::getOrdersId,item.getOrdersId())
                    .list());
        }
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:orders:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Orders orders)
    {
        List<Orders> list = ordersService.selectOrdersList(orders);
        ExcelUtil<Orders> util = new ExcelUtil<Orders>(Orders.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:orders:query')")
    @GetMapping(value = "/{ordersId}")
    public AjaxResult getInfo(@PathVariable("ordersId") Long ordersId)
    {
        Orders orders = ordersService.getById(ordersId);
        orders.setAccount(accountService.getById(orders.getAccountId()));
        orders.setOrdersItemVoList(ordersItemService.lambdaQuery().eq(OrdersItem::getDelFlag,"0").eq(OrdersItem::getOrdersId,ordersId).list());
        if (StringUtils.isNotEmpty(orders.getFetchBuilding())){
            Building building = buildingService.getById(orders.getFetchBuilding());
            Estate estate = estateService.getById(building.getEstateId());
            orders.setFetchEstate(estate.getName() + building.getName());
        }
        if (StringUtils.isNotEmpty(orders.getSendBuilding())){
            Building building = buildingService.getById(orders.getSendBuilding());
            Estate estate = estateService.getById(building.getEstateId());
            orders.setSendEstate(estate.getName() + building.getName());
        }
        return success(orders);
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('system:orders:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "courierId", value = "所属配送员", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "accountId", value = "所属用户", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "fetchName", value = "起点用户", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "fetchPhone", value = "起点电话", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "fetchBuilding", value = "起点楼栋", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "fetchDetails", value = "地点地址", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "sendName", value = "终点用户", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "sendPhone", value = "重点电话", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "sendBuilding", value = "终点楼栋", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "sendDetails", value = "终点地址", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "memo", value = "订单备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "specialMemo", value = "特殊要求", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "specialMemoImg", value = "特殊要求图片", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "productType", value = "物品类型", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "weight", value = "货物重量", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "receivingTime", value = "收货时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "money", value = "订单金额", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
                    @ApiImplicitParam(name = "originMoney", value = "订单原价", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
                    @ApiImplicitParam(name = "ordersNo", value = "订单编号", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "payNo", value = "支付单号", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "code", value = "收货编号", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "couponId", value = "使用优惠券", dataType = "Long", dataTypeClass = Long.class),
    })
    public AjaxResult add(@RequestBody Orders orders)
    {
        return toAjax(ordersService.insertOrders(orders));
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('system:orders:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "courierId", value = "所属配送员", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "accountId", value = "所属用户", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "fetchName", value = "起点用户", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "fetchPhone", value = "起点电话", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "fetchBuilding", value = "起点楼栋", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "fetchDetails", value = "地点地址", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "sendName", value = "终点用户", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "sendPhone", value = "重点电话", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "sendBuilding", value = "终点楼栋", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "sendDetails", value = "终点地址", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "memo", value = "订单备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "specialMemo", value = "特殊要求", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "specialMemoImg", value = "特殊要求图片", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "productType", value = "物品类型", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "weight", value = "货物重量", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "receivingTime", value = "收货时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "money", value = "订单金额", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
                    @ApiImplicitParam(name = "originMoney", value = "订单原价", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
                    @ApiImplicitParam(name = "ordersNo", value = "订单编号", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "payNo", value = "支付单号", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "code", value = "收货编号", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "couponId", value = "使用优惠券", dataType = "Long", dataTypeClass = Long.class),
    })
    public AjaxResult edit(@RequestBody Orders orders)
    {
        return toAjax(ordersService.updateOrders(orders));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('system:orders:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ordersIds}")
    public AjaxResult remove(@PathVariable Long[] ordersIds)
    {
        return toAjax(ordersService.deleteOrdersByOrdersIds(ordersIds));
    }


}
